/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package practica1;

import java.util.Stack;

/**
 *
 * @author 
 */
public class Nodo {
    public static int  numeroDeDatos=0;

    
    private Nodo liga1;
    private Nodo liga2;
    private char dato1;
    private char dato2;
    private boolean visit;
    private int name;

    Nodo(char dato1, char dato2){
        visit=false;
        this.dato1=dato1;
        this.dato2=dato2;
        liga1=liga2=null;
        if (this.dato2!='-') {
            numeroDeDatos++;
        }
    }

    public Nodo ultimo(){
        Nodo aux=this;
        Nodo anterior=aux;
        while(aux!=null){
            anterior=aux;
            aux=aux.getLiga1();
        }
        return anterior;
    }
    /**
     * @return the liga1
     */
    public Nodo getLiga1() {
        return liga1;
    }

    /**
     * @param liga1 the liga1 to set
     */
    public void setLiga1(Nodo liga1) {
        this.liga1 = liga1;
    }

    /**
     * @return the liga2
     */
    public Nodo getLiga2() {
        return liga2;
    }

    /**
     * @param liga2 the liga2 to set
     */
    public void setLiga2(Nodo liga2) {
        this.liga2 = liga2;
    }

    /**
     * @return the dato1
     */
    public char getDato1() {
        return dato1;
    }

    /**
     * @param dato1 the dato1 to set
     */
    public void setDato1(char dato1) {
        this.dato1 = dato1;
    }

    /**
     * @return the dato2
     */
    public char getDato2() {
        return dato2;
    }

    /**
     * @param dato2 the dato2 to set
     */
    public void setDato2(char dato2) {
        this.dato2 = dato2;
        numeroDeDatos++;
    }

    public void recorrer(Nodo aux){
        
        Stack pila = new Stack();
        while(!pila.isEmpty() || aux!=null){
            if(aux.getLiga2()!= null && !aux.getLiga2().isVisit()){
                pila.push(aux.getLiga2());
            }
            if(aux!= null && !aux.isVisit()){
                aux.setVisit(true);
            }
            aux = aux.getLiga1();
            if(aux==null && !pila.isEmpty()){
                aux = (Nodo)pila.pop();
            }
        }
    }

    public Nodo getNodo(int name){
        unVisit();
        Nodo aux = this;
        Stack pila = new Stack();
        while(!pila.isEmpty() || aux!=null){
            if(aux.getLiga2()!= null && !aux.getLiga2().isVisit()){
                pila.push(aux.getLiga2());
            }
            if(aux!= null && !aux.isVisit()){
                if(aux.getName()==name){
                    return aux;
                }
                aux.setVisit(true);
            }
            aux = aux.getLiga1();
            if(aux==null && !pila.isEmpty()){
                aux = (Nodo)pila.pop();
            }
            
        }
        unVisit();
        return aux;
    }

    /**
     * @return the visit
     */
    public boolean isVisit() {
        return visit;
    }

    /**
     * @param visit the visit to set
     */
    public void setVisit(boolean visit) {
        this.visit = visit;
    }

    /**
     * @return the name
     */
    public int getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(int name) {
        this.name = name;
    }

    /**
     * @return the numeroDeDatos
     */
    public static int getNumeroDeDatos() {
        return numeroDeDatos;
    }

    public String toString(){
        return String.valueOf(name);
    }

    public void unVisit(){
       Nodo aux=this;
       Stack pila = new Stack();
            while(!pila.isEmpty() || aux!=null){
                if(aux.getLiga2()!= null && aux.getLiga2().isVisit()){
                    pila.push(aux.getLiga2());
                }
                aux.setVisit(false);
                aux = aux.getLiga1();
                if(aux==null && !pila.isEmpty()){
                    aux = (Nodo)pila.pop();
                }
            }

   }
}
